﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.ContentHub.CustomFields
{
    public class DocumentTraceabilityFieldValue : SPFieldMultiColumnValue
    {
        public DocumentTraceabilityFieldValue(string value)
            : base(value) { }

        public IEnumerable<TraceabilityEntry> Entries
        {
            get
            {
                XmlSerializer tSerializer = new XmlSerializer(typeof(TraceabilityEntry));
                for (int i = 0; i < this.Count; i++)
                {
                    yield return tSerializer.Deserialize(new StringReader(this[i])) as TraceabilityEntry;                    
                }
            }
            set{
                int index = 0;
                XmlSerializer tSerializer = new XmlSerializer(typeof(TraceabilityEntry));
                StringWriter sWriter = new StringWriter();
                foreach (TraceabilityEntry entry in value) {
                    tSerializer.Serialize(sWriter, value);
                    this[index] = sWriter.ToString();
                    index++;
                }
            }
        }
    }
}
